﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LogProcess
{
    public enum CommandParameterType
    {
        FilePath,
        Folder,
        Integer,
        String        
    }
    
    public class CommandParameters
    {
        private List<CommandParameter> _parameters;

        public CommandParameters(List<CommandParameter> parameters)
        {
            _parameters = parameters;
        }
        public object this[string name]
        {
            get
            {
                var param = _parameters.FirstOrDefault( p => p.Name == name );
                if (param == null)
                    return null;

                return param.Value;
            }
        }
    }

    public class CommandParameter
    {
        public string Name;
        public CommandParameterType Type;
        public object Value { get; private set; }

        public CommandParameter() { }

        public string SetValue(string arg)
        {
            switch (Type)
            {
                case CommandParameterType.Integer:
                    int i;
                    if (!int.TryParse( arg, out i ))
                        return "Expected a number";
                    Value = i;
                    break;

                case CommandParameterType.FilePath:

                    if (!File.Exists( arg ))
                        return "File '" + arg + "' does not exist";

                    Value = arg;
                    break;

                case CommandParameterType.Folder:

                    if (!Directory.Exists( arg ))
                        return "Folder '" + arg + "' does not exist";

                    Value = arg;
                    break;

                case CommandParameterType.String:
                    Value = arg;
                    break;
            }

            return null;
        }
    }
}
